package com.beauty.dressing.module.mine.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.beauty.dressing.R;
import com.beauty.dressing.common.bean.AddressManagerBean;
import com.beauty.dressing.common.interfaces.OnRecyclerViewItemListener;

import java.util.ArrayList;
import java.util.List;


public class AddressManageAdapter extends RecyclerView.Adapter<AddressManageAdapter.AddressManageViewHolder> {
    private Context mContext;
    private List<AddressManagerBean> mList;
    private OnRecyclerViewItemListener listener;

    public AddressManageAdapter(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public AddressManageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.address_manage_item, parent, false);
        return new AddressManageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AddressManageViewHolder holder, final int position) {
        holder.address_name.setText(mList.get(position).getName());
        holder.address_phone.setText(mList.get(position).getPhone());
        holder.tv_address.setText(mList.get(position).getAddress());
        if (mList.get(position).getStatus() == 0) {
            holder.tv_default_address.setVisibility(View.INVISIBLE);
        }
        if (mList.size() - 1 == position) {
            holder.delimiter.setVisibility(View.GONE);
        }
        holder.iv_edit_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemViewOnClick(R.id.iv_edit_address, position);
            }
        });
        holder.address_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemViewOnClick(1, position);
            }
        });
        holder.address_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemViewOnClick(1, position);
            }
        });
        holder.tv_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemViewOnClick(1, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mList != null && !mList.isEmpty())
            return mList.size();
        else return 0;
    }

    public void setData(List<AddressManagerBean> list) {
        if (mList != null && !mList.isEmpty()) {
            mList.clear();
            mList.addAll(list);
        } else {
            mList = new ArrayList<>();
            mList.addAll(list);
        }
        this.notifyDataSetChanged();
    }


    public void setOnRecyclerViewItemListener(OnRecyclerViewItemListener listener) {
        this.listener = listener;
    }
//    public void setDefaultAddress(int position) {
//
//    }

    class AddressManageViewHolder extends RecyclerView.ViewHolder {
        TextView address_name;//用户ID
        TextView address_phone;//用户手机号码
        TextView tv_default_address;//是否用户默认地址
        TextView tv_address;//地址
        ImageView iv_edit_address;//编辑地址
        View delimiter;//分隔符

        AddressManageViewHolder(View itemView) {
            super(itemView);
            address_name = (TextView) itemView.findViewById(R.id.address_name);
            address_phone = (TextView) itemView.findViewById(R.id.address_phone);
            tv_default_address = (TextView) itemView.findViewById(R.id.tv_default_address);
            tv_address = (TextView) itemView.findViewById(R.id.tv_address);
            iv_edit_address = (ImageView) itemView.findViewById(R.id.iv_edit_address);
            delimiter = itemView.findViewById(R.id.delimiter);

        }
    }
}
